<!DOCTYPE html>




  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">

<script>
    (function(){
        if('liuruijia3.14'){
            if (prompt('请输入文章密码') !== 'liuruijia3.14'){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>


  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-center-atom.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="数论,学习笔记," />










<meta name="description" content="1. 最大公约数和最小公倍数1.1 几个有用的公式 \mathrm {lcm}(S)=\prod_{T\subset S}\mathrm{gcd}(T)^{(-1)^{\left | T \right |+1}} \tag{1} \mathrm{gcd}(\mathrm{Fib}(a),\mathrm{Fib}(b))=\mathrm{Fib}(\mathrm{gcd}(a,b))\tag{2}">
<meta name="keywords" content="数论,学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="数论">
<meta property="og:url" content="http://yoursite.com/2018/04/08/数论/index.html">
<meta property="og:site_name" content="lrj998244353">
<meta property="og:description" content="1. 最大公约数和最小公倍数1.1 几个有用的公式 \mathrm {lcm}(S)=\prod_{T\subset S}\mathrm{gcd}(T)^{(-1)^{\left | T \right |+1}} \tag{1} \mathrm{gcd}(\mathrm{Fib}(a),\mathrm{Fib}(b))=\mathrm{Fib}(\mathrm{gcd}(a,b))\tag{2}">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-04-09T13:19:41.653Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数论">
<meta name="twitter:description" content="1. 最大公约数和最小公倍数1.1 几个有用的公式 \mathrm {lcm}(S)=\prod_{T\subset S}\mathrm{gcd}(T)^{(-1)^{\left | T \right |+1}} \tag{1} \mathrm{gcd}(\mathrm{Fib}(a),\mathrm{Fib}(b))=\mathrm{Fib}(\mathrm{gcd}(a,b))\tag{2}">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/04/08/数论/"/>





  <title>数论 | lrj998244353</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">lrj998244353</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">趁一切还来得及</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/04/08/数论/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lrj998244353">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lrj998244353">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数论</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-08T22:10:24+08:00">
                2018-04-08
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/04/08/数论/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/04/08/数论/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,841
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长(分钟) &asymp;</span>
                
                <span title="阅读时长(分钟)">
                  14
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-最大公约数和最小公倍数"><a href="#1-最大公约数和最小公倍数" class="headerlink" title="1. 最大公约数和最小公倍数"></a>1. 最大公约数和最小公倍数</h1><h2 id="1-1-几个有用的公式"><a href="#1-1-几个有用的公式" class="headerlink" title="1.1 几个有用的公式"></a>1.1 几个有用的公式</h2><script type="math/tex; mode=display">
\mathrm {lcm}(S)=\prod_{T\subset S}\mathrm{gcd}(T)^{(-1)^{\left | T \right |+1}} \tag{1}</script><script type="math/tex; mode=display">
\mathrm{gcd}(\mathrm{Fib}(a),\mathrm{Fib}(b))=\mathrm{Fib}(\mathrm{gcd}(a,b))\tag{2}</script><script type="math/tex; mode=display">
\mathrm{gcd}(x^a-1,x^b-1)=x^{\mathrm{gcd}(a,b)}-1\tag{3}</script><a id="more"></a>
<h2 id="1-2-说明"><a href="#1-2-说明" class="headerlink" title="1.2 说明"></a>1.2 说明</h2><h3 id="公式1"><a href="#公式1" class="headerlink" title="公式1"></a>公式1</h3><p>证明需要用到引理</p>
<script type="math/tex; mode=display">
\mathrm{max}(\mathrm{S})=\sum_{\mathrm{T} \subset\mathrm{S}}\mathrm{min(\mathrm{T})}(-1)^{\left | \mathrm{T}\right |+1}\tag{4}</script><p>这个东西有个名字叫$min-max$容斥，本质还是组合形式的容斥原理。</p>
<p>证明可以考虑某个元素，有$p$个元素比它大，我们求出它会被计算多少次。</p>
<p>显然，次数为</p>
<script type="math/tex; mode=display">
\sum_{i=1}^p \binom{p}{i} \mathrm{coef}_i</script><p>其中$\mathrm{coef}_i$为容斥系数，在该式中为 $(-1)^{\left | \mathrm{T} \right |+1 }$</p>
<p>代入后就是</p>
<script type="math/tex; mode=display">
\sum_{i=1}^p \binom{p}{i}  (-1)^{\left | \mathrm{T} \right |+1 }</script><p>由二项式定理，可以证明当且仅当$p=0$时上式的值为$1$也就是只有最大的数会被计算一次，其余的数不会被计算。</p>
<p>公式1的本质就是$min-max$容斥，只不过将其用到了指数上。</p>
<p>首先我们将所有数分解成质数幂的乘积的形式，那么最小公倍数就是对所有质数的指数取$max$，最大公约数就是对所有质数的指数取$min$。指数上的求和就是原数的求积。</p>
<h3 id="公式2"><a href="#公式2" class="headerlink" title="公式2"></a>公式2</h3><p>公式2说的是斐波那契数列是一个$Strong Divisibility Sequence$ </p>
<p>首先$Divisibility Sequence$的定义是对于任意$m|n$有$a_m|a_n$，那么就称数列$\{a_n\}$为$Divisibility Sequence$</p>
<p>而$Strong Divisibility Sequence$就是在$Divisibility Sequence$的基础上还满足$\mathrm{gcd}(a_m,a_n)=a_{\mathrm{gcd}(m,n)}$</p>
<p>除了斐波那契数列外，下面这几个数列也是常见的$Strong Divisibility Sequence$ </p>
<ol>
<li><p>常数列</p>
</li>
<li><p>通项为$a_n=kn$的数列，$k$是常数</p>
</li>
<li><p>通项为$a_n=A^n-B^n$的数列，$A$和$B$是常数</p>
</li>
<li><p>佩尔数</p>
<script type="math/tex; mode=display">
P_n=\begin{cases}
0 & \text{ if } n=0 ;\\ 
1 & \text{ if } n=1 ;\\ 
2P_{n-1}+P_{n-2} & \text{ otherwise. }
\end{cases}
\tag{5}</script></li>
</ol>
<h3 id="公式3"><a href="#公式3" class="headerlink" title="公式3"></a>公式3</h3><p>不妨设$b&gt;a$，那么显然有</p>
<script type="math/tex; mode=display">
x^b-1=x^a(x^{b-a}-1)+(x^a-1)</script><p>利用该式将原式化简</p>
<script type="math/tex; mode=display">
\begin{align}
\mathrm{gcd}(x^a-1,x^b-1) &=\mathrm{gcd}(x^a-1,x^{b-a}-1)\\
                       &=\mathrm{gcd}(x^a-1,x^{b-2a}-1)\\
                       &\vdots \\
                       &=\mathrm{gcd}(x^a-1,x^{b\%a}-1)\\
                       &\vdots \\
                       &=x^{\mathrm{gcd}(a,b)}-1\\               
\end{align}</script><p>证明的过程用到了类似辗转相除法的方法。</p>
<h2 id="1-3-应用"><a href="#1-3-应用" class="headerlink" title="1.3 应用"></a>1.3 应用</h2><p>我们试着用上面三个公式做一些题。</p>
<h3 id="bzoj4833-Lydsy1704月赛-最小公倍佩尔数"><a href="#bzoj4833-Lydsy1704月赛-最小公倍佩尔数" class="headerlink" title="bzoj4833: [Lydsy1704月赛]最小公倍佩尔数"></a>bzoj4833: [Lydsy1704月赛]最小公倍佩尔数</h3><h4 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h4><p> 令(1+sqrt(2))^n=e(n)+f(n)*sqrt(2),其中e(n),f(n)都是整数，显然有(1-sqrt(2))^n=e(n)-f(n)*sqrt(2)。令g(</p>
<p>n)表示f(1),f(2)…f(n)的最小公倍数，给定两个正整数n和p,其中p是质数，并且保证f(1),f(2)…f(n)在模p意义</p>
<p>下均不为0,请计算sigma(i*g(i)),1&lt;=i&lt;=n.其在模p的值。</p>
<h4 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h4><p>第一行包含一个正整数 T ，表示有 T 组数据，满足 T≤210 。接下来是测试数据。每组测试数据只占一行，包含</p>
<p>两个正整数 n 和 p ，满足 1≤n≤10^6,2≤p≤10^9+7 。保证所有测试数据的 n 之和不超过 3×10^6  。</p>
<h4 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h4><p>对于每组测试数据，输出一行一个非负整数，表示这组数据的答案。</p>
<h4 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">5</span><br><span class="line">1 233</span><br><span class="line">2 233</span><br><span class="line">3 233</span><br><span class="line">4 233</span><br><span class="line">5 233</span><br></pre></td></tr></table></figure>
<h4 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">5</span><br><span class="line">35</span><br><span class="line">42</span><br><span class="line">121</span><br></pre></td></tr></table></figure>
<h4 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h4><p>题目要求的是佩尔数前$n$项的最小公倍数。由$(1)$式可以得到，</p>
<script type="math/tex; mode=display">
g(n)=\prod_{T\subset 2^{[n]}}\mathrm{gcd}_{i\in T}(f(i))^{(-1)^{\left | T\right |+1}}</script><p>连乘符号下的$T\subset 2^{[n]}$表示枚举这$n$个数构成的集合的所有子集。</p>
<p>由于佩尔数是$Strong Divisibility Sequence$ ，因此</p>
<script type="math/tex; mode=display">
g(n)=\prod_{T\subset 2^{[n]}}f(\mathrm{gcd}_{i\in T}(i))^{(-1)^{\left | T\right |+1}}\tag{6}</script><p>构造数列$h$使得</p>
<script type="math/tex; mode=display">
f(n)=\prod_{d|n}h(d)</script><p>代入$(6)$式</p>
<script type="math/tex; mode=display">
\begin{align}
g(n) &=\prod_{T\subset 2^{[n]}}(\prod_{d|\mathrm{gcd_{i\in T}(i)}}h(d))^{(-1)^{\left | T\right |+1}} \\
        &=\prod_{d=1}^nh(d)^{\sum_{T\subset2^{[n/d]}}(-1)^{\left | T\right |+1}}\\
        &=\prod_{d=1}^nh(d)
\end{align}</script><p>第二步到第三步用到的式子$\sum_{T\subset2^{[n/d]}}(-1)^{\left | T\right |+1}=1$可以用二项式定理证。</p>
<p>答案就是$h$的前缀和。</p>
<p>根据定义</p>
<script type="math/tex; mode=display">
h(n)=f(n)(\prod_{d|n,d\neq n}h(d))^{-1}</script><p>因此可以用类似筛法的方法筛出来。时间复杂度是调和级数。</p>
<h4 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 1000010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long</span></span><br><span class="line"><span class="keyword">namespace</span> IO&#123;</span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1</span>&lt;&lt;<span class="number">15</span>],*fs,*ft;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">char</span> <span class="title">gc</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> (fs==ft&amp;&amp;(ft=(fs=buf)+fread(buf,<span class="number">1</span>,<span class="number">1</span>&lt;&lt;<span class="number">15</span>,<span class="built_in">stdin</span>),fs==ft))?<span class="number">0</span>:*fs++;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">qr</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> x=<span class="number">0</span>,rev=<span class="number">0</span>,ch=gc();</span><br><span class="line">		<span class="keyword">while</span>(ch&lt;<span class="string">'0'</span>||ch&gt;<span class="string">'9'</span>)&#123;<span class="keyword">if</span>(ch==<span class="string">'-'</span>)rev=<span class="number">1</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">while</span>(ch&gt;=<span class="string">'0'</span>&amp;&amp;ch&lt;=<span class="string">'9'</span>)&#123;x=(x&lt;&lt;<span class="number">1</span>)+(x&lt;&lt;<span class="number">3</span>)+ch-<span class="string">'0'</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">return</span> rev?-x:x;&#125;</span><br><span class="line">&#125;<span class="keyword">using</span> <span class="keyword">namespace</span> IO;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> T,N,P;</span><br><span class="line">LL h[MAXN],f[MAXN];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> LL <span class="title">qp</span><span class="params">(LL x,<span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">	LL ret=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y&amp;<span class="number">1</span>)ret=ret*x%P;</span><br><span class="line">		x=x*x%P;y&gt;&gt;=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">ifndef</span> ONLINE_JUDGE</span></span><br><span class="line">	freopen(<span class="string">"bzoj4833.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">	freopen(<span class="string">"bzoj4833.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">	T=qr();</span><br><span class="line">	f[<span class="number">0</span>]=<span class="number">0</span>;f[<span class="number">1</span>]=h[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(T--)&#123;</span><br><span class="line">		N=qr();P=qr();</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=N;i++)f[i]=((f[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>)%P+f[i<span class="number">-2</span>])%P,h[i]=f[i];</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=N;i++)&#123;</span><br><span class="line">			LL inv=qp(h[i],P<span class="number">-2</span>);</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> j=i+i;j&lt;=N;j+=i)&#123;</span><br><span class="line">				h[j]=h[j]*inv%P; </span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		LL lcm=<span class="number">1</span>,ans=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=N;i++)lcm=lcm*h[i]%P,ans=(ans+lcm*i%P)%P;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="51nod1355-斐波那契的最小公倍数"><a href="#51nod1355-斐波那契的最小公倍数" class="headerlink" title="51nod1355 斐波那契的最小公倍数"></a>51nod1355 斐波那契的最小公倍数</h3><p>斐波那契数列定义如下：</p>
<p>F(0) = 0 F(1) = 1<br>F(n) = F(n-1) + F(n-2)</p>
<p>给出n个正整数a1, a2,…… an，求对应的斐波那契数的最小公倍数，由于数字很大，输出Mod 1000000007的结果即可。</p>
<p>例如：1 3 6 9, 对应的斐波那契数为：1 2 8 34, 他们的最小公倍数为136。</p>
<h4 id="Input-1"><a href="#Input-1" class="headerlink" title="Input"></a>Input</h4><p>第1行：1个数N，表示数字的数量（2 &lt;= N &lt;= 50000）。<br>第2 至 N + 1行：每行1个数，对应ai。（1 &lt;= ai &lt;= 1000000)。</p>
<h4 id="Output-1"><a href="#Output-1" class="headerlink" title="Output"></a>Output</h4><p>输出Lcm(F(a1), F(a2) …… F(an)) Mod 1000000007的结果。</p>
<h4 id="Input示例"><a href="#Input示例" class="headerlink" title="Input示例"></a>Input示例</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">4</span><br><span class="line">1</span><br><span class="line">3</span><br><span class="line">6</span><br><span class="line">9</span><br></pre></td></tr></table></figure>
<h4 id="Output示例"><a href="#Output示例" class="headerlink" title="Output示例"></a>Output示例</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">136</span><br></pre></td></tr></table></figure>
<h4 id="题解-1"><a href="#题解-1" class="headerlink" title="题解"></a>题解</h4><p>和上一题解法类似。只不过给定了项数集合。最后推出来的式子为</p>
<script type="math/tex; mode=display">
\mathrm{lcm}(f_S)=\prod _{\exists a\in S,d|a}h_d</script><p>$h_d$依然是我们构造出来的数列，满足</p>
<script type="math/tex; mode=display">
f_n=\prod_{d|n}h_d</script><p>依然是枚举$d$，判断给出的集合中是否有$d$的倍数，如果有，就把答案乘上$h_d$</p>
<h4 id="代码-1"><a href="#代码-1" class="headerlink" title="代码"></a>代码</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 1000010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> P 1000000007</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long </span></span><br><span class="line"><span class="keyword">namespace</span> IO&#123;</span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1</span>&lt;&lt;<span class="number">15</span>],*fs,*ft;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">char</span> <span class="title">gc</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> (fs==ft&amp;&amp;(ft=(fs=buf)+fread(buf,<span class="number">1</span>,<span class="number">1</span>&lt;&lt;<span class="number">15</span>,<span class="built_in">stdin</span>),fs==ft))?<span class="number">0</span>:*fs++;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">qr</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> x=<span class="number">0</span>,rev=<span class="number">0</span>,ch=gc();</span><br><span class="line">		<span class="keyword">while</span>(ch&lt;<span class="string">'0'</span>||ch&gt;<span class="string">'9'</span>)&#123;<span class="keyword">if</span>(ch==<span class="string">'-'</span>)rev=<span class="number">1</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">while</span>(ch&gt;=<span class="string">'0'</span>&amp;&amp;ch&lt;=<span class="string">'9'</span>)&#123;x=(x&lt;&lt;<span class="number">1</span>)+(x&lt;&lt;<span class="number">3</span>)+ch-<span class="string">'0'</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">return</span> rev?-x:x;&#125;</span><br><span class="line">&#125;<span class="keyword">using</span> <span class="keyword">namespace</span> IO;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> N,a[MAXN],n;</span><br><span class="line">LL f[MAXN],h[MAXN],ans=<span class="number">1</span>;</span><br><span class="line"><span class="function">LL <span class="title">qp</span><span class="params">(LL x,<span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">	LL ret=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y&amp;<span class="number">1</span>)ret=ret*x%P;</span><br><span class="line">		x=x*x%P;y&gt;&gt;=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">int</span> x;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">ifndef</span> ONLINE_JUDGE</span></span><br><span class="line">	freopen(<span class="string">"51nod1355.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">	freopen(<span class="string">"51nod1355.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">	N=qr();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=N;i++)x=qr(),n=max(n,x),a[x]=<span class="number">1</span>;</span><br><span class="line">	f[<span class="number">0</span>]=<span class="number">0</span>;f[<span class="number">1</span>]=h[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i++)f[i]=(f[i<span class="number">-1</span>]+f[i<span class="number">-2</span>])%P,h[i]=f[i];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		LL inv=qp(h[i],P<span class="number">-2</span>);</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=i+i;j&lt;=n;j+=i)h[j]=h[j]*inv%P;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=i;j&lt;=n;j+=i)&#123;</span><br><span class="line">			<span class="keyword">if</span>(a[j])&#123;</span><br><span class="line">				ans=ans*h[i]%P;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="HDU5780-gcd"><a href="#HDU5780-gcd" class="headerlink" title="HDU5780 gcd"></a>HDU5780 gcd</h3><h4 id="Problem-Description"><a href="#Problem-Description" class="headerlink" title="Problem Description"></a>Problem Description</h4><p>Little White learned the greatest common divisor, so she plan to solve a problem: given x, n，<br>query $\sum \mathrm{gcd}(x^a−1,x^b−1)$ (1≤a,b≤n) </p>
<h4 id="Input-2"><a href="#Input-2" class="headerlink" title="Input"></a>Input</h4><p>The first line of input is an integer T ( 1≤T≤300)<br>For each test case ，the single line contains two integers x and n ( 1≤x,n≤1000000)</p>
<h4 id="Output-2"><a href="#Output-2" class="headerlink" title="Output"></a>Output</h4><p>For each testcase, output a line, the answer mod 1000000007 </p>
<h4 id="Sample-Input-1"><a href="#Sample-Input-1" class="headerlink" title="Sample Input"></a>Sample Input</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">5</span><br><span class="line">3 1</span><br><span class="line">4 2</span><br><span class="line">8 7</span><br><span class="line">10 5</span><br><span class="line">10 8</span><br></pre></td></tr></table></figure>
<h4 id="Sample-Output-1"><a href="#Sample-Output-1" class="headerlink" title="Sample Output"></a>Sample Output</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">2</span><br><span class="line">24</span><br><span class="line">2398375</span><br><span class="line">111465</span><br><span class="line">111134466</span><br></pre></td></tr></table></figure>
<h4 id="题解-2"><a href="#题解-2" class="headerlink" title="题解"></a>题解</h4><p>题目相当于求</p>
<script type="math/tex; mode=display">
\sum_{1\leq a,b\leq n}(x^{\mathrm{gcd}(a,b)}-1)</script><p> 枚举最大公约数</p>
<script type="math/tex; mode=display">
\sum_{d=1}^n(x^d-1)\sum_{1\leq a,b\leq n}[\mathrm{gcd}(a,b)=k]</script><p>后面的部分单独拿出来</p>
<script type="math/tex; mode=display">
\begin{align}
&\sum_{1\leq a,b\leq n}[\mathrm{gcd}(a,b)=k]\\
=&\sum_{1\leq a,b\leq n/k}[\mathrm{gcd}(a,b)=1]\\
=&2\sum_{i=1}^{\left \lfloor\frac{n}{k}  \right \rfloor}\varphi(i)-1
\end{align}</script><p>因此答案就是</p>
<script type="math/tex; mode=display">
ans=\sum_{d=1}^n(x^d-1)\left ( 2\sum_{i=1}^{\left \lfloor\frac{n}{k}  \right \rfloor}\varphi(i)-1 \right )</script><p>这东西可以分块算，预处理欧拉函数前缀和。每一块前半部分用等比数列求和公式。需要注意$x=1$的情况。</p>
<h4 id="代码-2"><a href="#代码-2" class="headerlink" title="代码"></a>代码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 1000010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> P 1000000007</span></span><br><span class="line"><span class="keyword">namespace</span> IO&#123;</span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1</span>&lt;&lt;<span class="number">15</span>],*fs,*ft;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">char</span> <span class="title">gc</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> (fs==ft&amp;&amp;(ft=(fs=buf)+fread(buf,<span class="number">1</span>,<span class="number">1</span>&lt;&lt;<span class="number">15</span>,<span class="built_in">stdin</span>),fs==ft))?<span class="number">0</span>:*fs++;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">qr</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> x=<span class="number">0</span>,rev=<span class="number">0</span>,ch=gc();</span><br><span class="line">		<span class="keyword">while</span>(ch&lt;<span class="string">'0'</span>||ch&gt;<span class="string">'9'</span>)&#123;<span class="keyword">if</span>(ch==<span class="string">'-'</span>)rev=<span class="number">1</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">while</span>(ch&gt;=<span class="string">'0'</span>&amp;&amp;ch&lt;=<span class="string">'9'</span>)&#123;x=(x&lt;&lt;<span class="number">1</span>)+(x&lt;&lt;<span class="number">3</span>)+ch-<span class="string">'0'</span>;ch=gc();&#125;</span><br><span class="line">		<span class="keyword">return</span> rev?-x:x;&#125;</span><br><span class="line">&#125;<span class="keyword">using</span> <span class="keyword">namespace</span> IO;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> T,p[MAXN],N,cnt,phi[MAXN];</span><br><span class="line">LL sum[MAXN],ans,inv;</span><br><span class="line"><span class="keyword">bool</span> np[MAXN];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> LL <span class="title">qp</span><span class="params">(LL x,<span class="keyword">int</span> y)</span></span>&#123;</span><br><span class="line">	LL ret=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y&amp;<span class="number">1</span>)ret=ret*x%P;</span><br><span class="line">		x=x*x%P;y&gt;&gt;=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">int</span> x;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">ifndef</span> ONLINE_JUDGE</span></span><br><span class="line">	freopen(<span class="string">"hdu5780.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">	freopen(<span class="string">"hdu5780.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">	sum[<span class="number">1</span>]=phi[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=<span class="number">1000000</span>;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(!np[i])p[++cnt]=i,phi[i]=i<span class="number">-1</span>;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=cnt;j++)&#123;</span><br><span class="line">			<span class="keyword">int</span> t=i*p[j];</span><br><span class="line">			<span class="keyword">if</span>(t&gt;<span class="number">1000000</span>)<span class="keyword">break</span>;</span><br><span class="line">			np[t]=<span class="number">1</span>;</span><br><span class="line">			<span class="keyword">if</span>(i%p[j]==<span class="number">0</span>)&#123;phi[t]=phi[i]*p[j];<span class="keyword">break</span>;&#125;</span><br><span class="line">			phi[t]=phi[i]*(p[j]<span class="number">-1</span>); </span><br><span class="line">		&#125;</span><br><span class="line">		sum[i]=(sum[i<span class="number">-1</span>]+phi[i])%P;</span><br><span class="line">	&#125;</span><br><span class="line">	T=qr();</span><br><span class="line">	<span class="keyword">while</span>(T--)&#123;</span><br><span class="line">		x=qr();N=qr();</span><br><span class="line">		<span class="keyword">if</span>(x==<span class="number">1</span>)&#123;<span class="built_in">puts</span>(<span class="string">"0"</span>);<span class="keyword">continue</span>;&#125;</span><br><span class="line">		inv=qp(x<span class="number">-1</span>,P<span class="number">-2</span>);ans=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>,j;i&lt;=N;i=j+<span class="number">1</span>)&#123;</span><br><span class="line">			j=N/(N/i);</span><br><span class="line">			ans=(ans+qp(x,i)*inv%P*(qp(x,j-i+<span class="number">1</span>)<span class="number">-1</span>+P)%P*(sum[N/i]*<span class="number">2</span>%P<span class="number">-1</span>+P)%P+P)%P;</span><br><span class="line">		&#125;</span><br><span class="line">		ans=((ans-(LL)N*N%P)%P+P)%P;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="2-费马小定理、欧拉定理、扩展欧拉定理"><a href="#2-费马小定理、欧拉定理、扩展欧拉定理" class="headerlink" title="2. 费马小定理、欧拉定理、扩展欧拉定理"></a>2. 费马小定理、欧拉定理、扩展欧拉定理</h1><h2 id="2-1-定理内容"><a href="#2-1-定理内容" class="headerlink" title="2.1 定理内容"></a>2.1 定理内容</h2><h3 id="2-1-1-费马小定理"><a href="#2-1-1-费马小定理" class="headerlink" title="2.1.1 费马小定理"></a>2.1.1 费马小定理</h3><p>若$p$是质数，且$a$不是$p$的倍数，则</p>
<script type="math/tex; mode=display">
a^{p-1}\equiv1\ (\mathrm{mod}\ p)</script><h3 id="2-1-2-欧拉定理"><a href="#2-1-2-欧拉定理" class="headerlink" title="2.1.2 欧拉定理"></a>2.1.2 欧拉定理</h3><p>若$a,p$互质，则</p>
<script type="math/tex; mode=display">
a^{\varphi(p)}\equiv1\ (\mathrm{mod}\ p)</script><h3 id="2-1-3-扩展欧拉定理"><a href="#2-1-3-扩展欧拉定理" class="headerlink" title="2.1.3 扩展欧拉定理"></a>2.1.3 扩展欧拉定理</h3><p>一般情况下</p>
<script type="math/tex; mode=display">
a^m\equiv a^{\mathrm{min} \left \{ m,(m\ \mathrm{mod\ \varphi(p)})+\varphi(p) \right \}}\ (\mathrm{mod}\ p)</script><h2 id="2-2-应用"><a href="#2-2-应用" class="headerlink" title="2.2 应用"></a>2.2 应用</h2><h3 id="bzoj3884-上帝与集合的正确用法"><a href="#bzoj3884-上帝与集合的正确用法" class="headerlink" title="bzoj3884: 上帝与集合的正确用法"></a>bzoj3884: 上帝与集合的正确用法</h3><h4 id="Description-1"><a href="#Description-1" class="headerlink" title="Description"></a>Description</h4><p>求</p>
<script type="math/tex; mode=display">
2^{2^{2^{2^{2^{2^{2\cdots}}}}}}\ (\mathrm{mod}\ p)</script><p>的值。</p>
<h4 id="Input-3"><a href="#Input-3" class="headerlink" title="Input"></a>Input</h4><p>接下来T行，每行一个正整数p，代表你需要取模的值</p>
<h4 id="Output-3"><a href="#Output-3" class="headerlink" title="Output"></a>Output</h4><p>T行，每行一个正整数，为答案对p取模后的值</p>
<h4 id="Sample-Input-2"><a href="#Sample-Input-2" class="headerlink" title="Sample Input"></a>Sample Input</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">6</span><br></pre></td></tr></table></figure>
<h4 id="Sample-Output-2"><a href="#Sample-Output-2" class="headerlink" title="Sample Output"></a>Sample Output</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">0</span><br><span class="line">1</span><br><span class="line">4</span><br></pre></td></tr></table></figure>
<h4 id="HINT"><a href="#HINT" class="headerlink" title="HINT"></a>HINT</h4><p>对于100%的数据，T&lt;=1000,p&lt;=10^7</p>
<h4 id="题解-3"><a href="#题解-3" class="headerlink" title="题解"></a>题解</h4><p>设答案为$ans(p)$，根据扩展欧拉定理，有</p>
<script type="math/tex; mode=display">
\begin{align}
&ans(p)\\
=&2^{2^{2^\cdots}}\ (\mathrm{mod}\ p)\\
=&2^{\left ( 2^{2\cdots}\right)\mathrm{mod}\ \varphi(p)+\varphi(p)}\ (\mathrm{mod}\ p)\\
=&2^{ans(\varphi(p))+\varphi(p)}\ (\mathrm{mod}\ p)
\end{align}</script><p>递归求解即可。</p>
<h4 id="代码-3"><a href="#代码-3" class="headerlink" title="代码"></a>代码</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 10000010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> T,P;</span><br><span class="line"><span class="function">LL <span class="title">qp</span><span class="params">(LL x,<span class="keyword">int</span> y,<span class="keyword">int</span> p)</span></span>&#123;</span><br><span class="line">	LL ret=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y&amp;<span class="number">1</span>)ret=ret*x%p;</span><br><span class="line">		x=x*x%p;y&gt;&gt;=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">Phi</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> ret=x;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i*i&lt;=x;i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(x%i==<span class="number">0</span>)&#123;</span><br><span class="line">			ret=ret/i*(i<span class="number">-1</span>);</span><br><span class="line">			<span class="keyword">while</span>(x%i==<span class="number">0</span>)x/=i;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(x&gt;<span class="number">1</span>)ret=ret/x*(x<span class="number">-1</span>); </span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Solve</span><span class="params">(<span class="keyword">int</span> p)</span></span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(p==<span class="number">1</span>)<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> phi=Phi(p);</span><br><span class="line">	<span class="keyword">return</span> qp(<span class="number">2</span>,Solve(phi)+phi,p);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">ifndef</span> ONLINE_JUDGE</span></span><br><span class="line">	freopen(<span class="string">"bzoj3884.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">	freopen(<span class="string">"bzoj3884.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</span><br><span class="line">	<span class="keyword">while</span>(T--)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;P);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"%d\n"</span>,Solve(P));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="3-中国剩余定理"><a href="#3-中国剩余定理" class="headerlink" title="3. 中国剩余定理"></a>3. 中国剩余定理</h1><h2 id="3-1-定理内容"><a href="#3-1-定理内容" class="headerlink" title="3.1 定理内容"></a>3.1 定理内容</h2><p>求一元模线性方程组$x\equiv a_i (\mathrm{mod} p_i)$的一个通解，要求$p_i$两两互质。</p>
<p>设</p>
<script type="math/tex; mode=display">
\begin{align}
P&=\prod p_i\\
P_i&=\frac{P}{p_i}\\
t_i&=P_i^{-1} (\mathrm{mod}\ p_i)
\end{align}</script><p>则通解为</p>
<script type="math/tex; mode=display">
x\equiv\sum a_it_iP_i \ (\mathrm{mod}\ P)</script><h1 id="4-扩展中国剩余定理"><a href="#4-扩展中国剩余定理" class="headerlink" title="4. 扩展中国剩余定理"></a>4. 扩展中国剩余定理</h1><h2 id="4-1-定理内容"><a href="#4-1-定理内容" class="headerlink" title="4.1 定理内容"></a>4.1 定理内容</h2><p>求一元模线性方程组$x\equiv a_i (\mathrm{mod} p_i)$的一个通解。</p>
<p>将方程两两合并，假如要合并如下两个方程</p>
<script type="math/tex; mode=display">
x\equiv a_1\ (\mathrm{mod}\ p_1)\\
x\equiv a_2\ (\mathrm{mod}\ p_2)</script><p>写成带余除法的形式</p>
<script type="math/tex; mode=display">
x=x_1p_1+a_1\\
x=x_2p_2+a_2</script><p>联立</p>
<script type="math/tex; mode=display">
x_1p_1+a_1=x_2p_2+a_2</script><p>移项</p>
<script type="math/tex; mode=display">
x_1p_1+x_2p_2=a_2-a_1</script><p>用扩展欧几里得算法解出$x_1$的最小正整数解，将其带回$x=x_1p_1+a_1$即可得到$x$的一个特解$x’$，显然也是最小正整数解。</p>
<p>$x$的通解一定是$x’+k\times\mathrm{lcm}(p_1,p_2)$，这样才能保证模$p_1$和$p_2$时的余数依然不变。</p>
<p>我们把特解$x’$作为新方程的余数，$\mathrm{lcm}(p_1,p_2)$作为新方程的模数，便完成了合并。</p>
<p>新方程为</p>
<script type="math/tex; mode=display">
x\equiv x'\ (\mathrm{mod}\ \mathrm{lcm}(p_1,p_2))</script><h1 id="5-筛法"><a href="#5-筛法" class="headerlink" title="5. 筛法"></a>5. 筛法</h1><h2 id="5-1-内容"><a href="#5-1-内容" class="headerlink" title="5.1 内容"></a>5.1 内容</h2><ul>
<li>埃拉托斯特尼筛法：枚举倍数</li>
<li>欧拉筛法：枚举最小质因子及其次数</li>
</ul>
<h2 id="5-2-应用"><a href="#5-2-应用" class="headerlink" title="5.2 应用"></a>5.2 应用</h2><p>线性筛积性函数。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数论/" rel="tag"><i class="fa fa-tag"></i> 数论</a>
          
            <a href="/tags/学习笔记/" rel="tag"><i class="fa fa-tag"></i> 学习笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/04/07/hello-world/" rel="next" title="Hello World">
                <i class="fa fa-chevron-left"></i> Hello World
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/04/09/加密测试/" rel="prev" title="加密测试">
                加密测试 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="lrj998244353" />
            
              <p class="site-author-name" itemprop="name">lrj998244353</p>
              <p class="site-description motion-element" itemprop="description">HAOIer</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-最大公约数和最小公倍数"><span class="nav-number">1.</span> <span class="nav-text">1. 最大公约数和最小公倍数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-几个有用的公式"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 几个有用的公式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-说明"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 说明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#公式1"><span class="nav-number">1.2.1.</span> <span class="nav-text">公式1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#公式2"><span class="nav-number">1.2.2.</span> <span class="nav-text">公式2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#公式3"><span class="nav-number">1.2.3.</span> <span class="nav-text">公式3</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-应用"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#bzoj4833-Lydsy1704月赛-最小公倍佩尔数"><span class="nav-number">1.3.1.</span> <span class="nav-text">bzoj4833: [Lydsy1704月赛]最小公倍佩尔数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Description"><span class="nav-number">1.3.1.1.</span> <span class="nav-text">Description</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Input"><span class="nav-number">1.3.1.2.</span> <span class="nav-text">Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Output"><span class="nav-number">1.3.1.3.</span> <span class="nav-text">Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Input"><span class="nav-number">1.3.1.4.</span> <span class="nav-text">Sample Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Output"><span class="nav-number">1.3.1.5.</span> <span class="nav-text">Sample Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#题解"><span class="nav-number">1.3.1.6.</span> <span class="nav-text">题解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码"><span class="nav-number">1.3.1.7.</span> <span class="nav-text">代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#51nod1355-斐波那契的最小公倍数"><span class="nav-number">1.3.2.</span> <span class="nav-text">51nod1355 斐波那契的最小公倍数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Input-1"><span class="nav-number">1.3.2.1.</span> <span class="nav-text">Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Output-1"><span class="nav-number">1.3.2.2.</span> <span class="nav-text">Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Input示例"><span class="nav-number">1.3.2.3.</span> <span class="nav-text">Input示例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Output示例"><span class="nav-number">1.3.2.4.</span> <span class="nav-text">Output示例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#题解-1"><span class="nav-number">1.3.2.5.</span> <span class="nav-text">题解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码-1"><span class="nav-number">1.3.2.6.</span> <span class="nav-text">代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HDU5780-gcd"><span class="nav-number">1.3.3.</span> <span class="nav-text">HDU5780 gcd</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Problem-Description"><span class="nav-number">1.3.3.1.</span> <span class="nav-text">Problem Description</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Input-2"><span class="nav-number">1.3.3.2.</span> <span class="nav-text">Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Output-2"><span class="nav-number">1.3.3.3.</span> <span class="nav-text">Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Input-1"><span class="nav-number">1.3.3.4.</span> <span class="nav-text">Sample Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Output-1"><span class="nav-number">1.3.3.5.</span> <span class="nav-text">Sample Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#题解-2"><span class="nav-number">1.3.3.6.</span> <span class="nav-text">题解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码-2"><span class="nav-number">1.3.3.7.</span> <span class="nav-text">代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-费马小定理、欧拉定理、扩展欧拉定理"><span class="nav-number">2.</span> <span class="nav-text">2. 费马小定理、欧拉定理、扩展欧拉定理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-定理内容"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 定理内容</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-1-费马小定理"><span class="nav-number">2.1.1.</span> <span class="nav-text">2.1.1 费马小定理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-2-欧拉定理"><span class="nav-number">2.1.2.</span> <span class="nav-text">2.1.2 欧拉定理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-3-扩展欧拉定理"><span class="nav-number">2.1.3.</span> <span class="nav-text">2.1.3 扩展欧拉定理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-应用"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#bzoj3884-上帝与集合的正确用法"><span class="nav-number">2.2.1.</span> <span class="nav-text">bzoj3884: 上帝与集合的正确用法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Description-1"><span class="nav-number">2.2.1.1.</span> <span class="nav-text">Description</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Input-3"><span class="nav-number">2.2.1.2.</span> <span class="nav-text">Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Output-3"><span class="nav-number">2.2.1.3.</span> <span class="nav-text">Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Input-2"><span class="nav-number">2.2.1.4.</span> <span class="nav-text">Sample Input</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Sample-Output-2"><span class="nav-number">2.2.1.5.</span> <span class="nav-text">Sample Output</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#HINT"><span class="nav-number">2.2.1.6.</span> <span class="nav-text">HINT</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#题解-3"><span class="nav-number">2.2.1.7.</span> <span class="nav-text">题解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码-3"><span class="nav-number">2.2.1.8.</span> <span class="nav-text">代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-中国剩余定理"><span class="nav-number">3.</span> <span class="nav-text">3. 中国剩余定理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-定理内容"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 定理内容</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-扩展中国剩余定理"><span class="nav-number">4.</span> <span class="nav-text">4. 扩展中国剩余定理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-定理内容"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 定理内容</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-筛法"><span class="nav-number">5.</span> <span class="nav-text">5. 筛法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-内容"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 内容</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-应用"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 应用</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lrj998244353</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">博客总字数&#58;</span>
    
    <span title="博客总字数">2.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访问总量:<span id="busuanzi_value_site_uv"></span>次
</span>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  







  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/canvas_lines.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>


  
  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: true,
        notify: true,
        appId: 'RPcFavnFpsxSGvNlKvnOn2S9-gzGzoHsz',
        appKey: 'yxseOiHdszYdvd1i8zwNn1DG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>
  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>

